package de.arndt_plc.wakeonmagic;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.appwidget.AppWidgetProviderInfo;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


/**
 * Implementation of App Widget functionality.
 * App Widget Configuration implemented in {@link WakeOnMagicAppWidgetConfigureActivity WakeOnMagicAppWidgetConfigureActivity}
 */
public class WakeOnMagicAppWidgetProvider extends AppWidgetProvider {

    public static final String TAG = WakeOnMagicAppWidgetProvider.class.getSimpleName();
    private static final List<Integer> mAppWidgetIds = new ArrayList<Integer>();

    public static List<Integer> getWidgetIds(Context context) {
        List<Integer> result = new ArrayList<Integer>();
        AppWidgetManager widgetMgr = AppWidgetManager.getInstance(context);
        // get all widget providers in the given context
        List<AppWidgetProviderInfo> providers = widgetMgr.getInstalledProviders();
        // search for the matching class name
        for (AppWidgetProviderInfo provider : providers) {
            Log.d(TAG, "Provider: " + provider.provider.getClassName());
            if (provider.provider.getClassName().equals(WakeOnMagicAppWidgetProvider.class.getName())) {
                int[] ids = widgetMgr.getAppWidgetIds(provider.provider);
                if (ids != null) {
                    // append ids to result list
                    for (int i = 0; i < ids.length; ++i)
                        result.add((Integer) ids[i]);
                    break;
                }
            }
        }
        return result;
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple mWidgets active, so update all of them
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            if (!mAppWidgetIds.contains((Integer) appWidgetIds[i]))
                mAppWidgetIds.add((Integer) appWidgetIds[i]);
            updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // When the user deletes the widget, delete the preference associated with it.
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            WakeOnMagicAppWidgetConfigureFragment.delete(context, appWidgetIds[i]);
            mAppWidgetIds.remove((Integer) appWidgetIds[i]);
        }
    }

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        WakeOnMagicAppWidgetConfigureFragment.Prefs prefs = WakeOnMagicAppWidgetConfigureFragment.load(context, appWidgetId);
        CharSequence widgetText = prefs.Pc;

        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wake_on_magic_app_widget);

        Intent intent = new Intent(context, WakeOnMagicIntentService.class);
        intent.putExtra(WakeOnMagicIntentService.EXTRA_MAC, (String) prefs.Mac);
        intent.putExtra(WakeOnMagicIntentService.EXTRA_IP, (String) prefs.IpBroadcast);
        intent.setAction(WakeOnMagicIntentService.ACTION_WAKE_PC);

        PendingIntent pendingIntent = PendingIntent.getService(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        views.setTextViewText(R.id.appwidget_pc, widgetText);
        views.setOnClickPendingIntent(R.id.appwidget_pc, pendingIntent);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }


}


